#include "componentgeneral.h"
#include "librarymodel.h"
#include "qedacomponent.h"
#include "qedalibrary.h"

#include <QtCore/qdebug.h>
#include <QtGui/qcombobox.h>
#include <QtGui/qlineedit.h>

ComponentGeneral::ComponentGeneral(LibraryModel *libModel, QWidget *parent) :
    QFormLayout(parent)
{
    m_library = new QComboBox;
    for (int i = 0; i < libModel->libraries().count(); ++i)
    {
        m_library->addItem(libModel->libraries().at(i)->name(),
                           qVariantFromValue((void*)libModel->libraries().at(i)));
    }
    m_library->setCurrentIndex(0);
    connect(m_library, SIGNAL(currentIndexChanged(int)), this, SLOT(updateRefsAndGroups()));

    m_name = new QLineEdit;
    connect(m_name, SIGNAL(textEdited(QString)), this, SIGNAL(dataChanged()));

    m_reference = new QComboBox;
    m_reference->setEditable(true);
    connect(m_reference, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(referenceChanged(QString)));
    connect(m_reference, SIGNAL(editTextChanged(QString)),
            this, SLOT(referenceChanged(QString)));

    m_group = new QComboBox;
    m_group->setEditable(true);
    connect(m_group, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(groupChanged(QString)));
    connect(m_group, SIGNAL(editTextChanged(QString)),
            this, SLOT(groupChanged(QString)));

    addRow(tr("Library:"), m_library);
    addRow(tr("Component name:"), m_name);
    addRow(tr("Reference:"), m_reference);
    addRow(tr("Group:"), m_group);

    updateRefsAndGroups();
}

void ComponentGeneral::addLibrary(QedaLibrary *library)
{
    m_library->addItem(library->name(), qVariantFromValue((void*)library));
}

void ComponentGeneral::changeComponentType(int componentType)
{

}

void ComponentGeneral::dataFrom(QedaComponent *component)
{
    setLibrary(component->library());
    m_name->setText(component->name());
    setReference(component->reference());
    setGroup(component->group());
}

void ComponentGeneral::dataTo(QedaComponent *component)
{
    component->setName(m_name->text());
    component->setReference(m_reference->currentText());
    component->setGroup(m_group->currentText());
    component->setLibrary(library());
    component->update();
}

void ComponentGeneral::groupChanged(const QString &newGroup)
{
    if (newGroup != m_currGroup)
    {
        m_currGroup = newGroup;
        emit dataChanged();
    }
}

QedaLibrary *ComponentGeneral::library()
{
    const int i = m_library->currentIndex();
    return (QedaLibrary*)m_library->itemData(i).value<void *>();
}

void ComponentGeneral::setGroup(const QString &group)
{
    bool found = false;
    for (int i = 0; (i < m_group->count()) && !found; ++i)
    {
        if (m_group->itemText(i) == group)
        {
            m_group->setCurrentIndex(i);
            found = true;
        }
    }
    if (!found)
        m_group->setEditText(group);
    m_currGroup = group;
}

void ComponentGeneral::referenceChanged(const QString &newReference)
{
    if (newReference != m_currReference)
    {
        m_currReference = newReference;
        emit dataChanged();
    }
}

void ComponentGeneral::removeLibrary(QedaLibrary *library)
{
    for (int i = 0; i < m_library->count(); ++i)
    {
        QedaLibrary *boxLib = (QedaLibrary*)m_library->itemData(i).value<void *>();
        if (boxLib == library)
        {
            m_library->removeItem(i);
            break;
        }
    }
}

void ComponentGeneral::setLibrary(QedaLibrary *library)
{
    for (int i = 0; i < m_library->count(); ++i)
    {
        QedaLibrary *boxLib = (QedaLibrary*)m_library->itemData(i).value<void *>();
        if (boxLib == library)
        {
            m_library->setCurrentIndex(i);
            updateRefsAndGroups();
            break;
        }
    }
}

void ComponentGeneral::setReference(const QString &reference)
{
    bool found = false;
    for (int i = 0; (i < m_reference->count()) && !found; ++i)
    {
        if (m_reference->itemText(i) == reference)
        {
            m_reference->setCurrentIndex(i);
            found = true;
        }
    }
    if (!found)
        m_reference->setEditText(reference);
    m_currReference = reference;
}

void ComponentGeneral::updateRefsAndGroups()
{
    QedaLibrary *lib = (QedaLibrary*)m_library->itemData(m_library->currentIndex()).value<void *>();
    if (lib)
    {
        QStringList refs = lib->references();
        const QString currRef = m_reference->currentText();
        m_reference->clear();
        for (int i = 0; i < refs.count(); ++i)
            m_reference->addItem(refs.at(i));
        setReference(currRef);

        QStringList gs = lib->groups();
        const QString currGroup = m_group->currentText();
        m_group->clear();
        for (int i = 0; i < gs.count(); ++i)
            m_group->addItem(gs.at(i));
        setGroup(currGroup);
    }
}

bool ComponentGeneral::valid()
{
    bool isValid = true;
    isValid = isValid && (!m_name->text().isEmpty());
    isValid = isValid && (!m_reference->currentText().isEmpty());
    isValid = isValid && (!m_group->currentText().isEmpty());
    return isValid;
}
